// pages/supply-search/supply-search.js
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    activeNames: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
    params:{
      foodName:'',
      sign:0,
    },
    lsit:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    

    const {
      estimatedTime,
      estimatedNum,
      applyType,
      stockOrderId,
      auxiliaryMaterialsId,
      smallCanteenPurchaseId,
      collarId
    } = app.globalData

    if (applyType == 0) {
      this.setData({ orderId: stockOrderId })
    } else if (applyType == 1) {
      this.setData({ orderId: auxiliaryMaterialsId })
    } else if (applyType == 2) {
      this.setData({ orderId: smallCanteenPurchaseId })
    } else {
      this.setData({ orderId: collarId })
    }

    if (applyType === 1) {
      this.setData({
        'params.type': "2"
      })
    }
    this.setData({
      applyType,
    })
  },
  changeCollapse(event) {
    this.setData({
      activeNames: event.detail
    });
  },
  onInput(e) {
    this.setData({
      'params.foodName': e.detail.value
    })
  },

  toSearch() {
    let params=this.data.params
    params.sign = this.data.applyType
    if (app.globalData.revise) {
      params.orderId = this.data.orderId
    }
    app.reqFetch.getSupplierName({ ...params}, (res) => {
        console.log(res)
      const { list=[]}=res
      this.setData({ list})
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  checkItem(e) {

    let { list } = this.data
    const foodItem = e.currentTarget.dataset.record
    list.map(item => {
      if (item.supplierId === foodItem.supplierId){
        item.foodDTO.map(ele=>{
          if (ele.foodId === foodItem.foodId) ele.flag = e.detail ? 1 : 0
        })
      }
    })
    this.setData({ list }, () => {
      if (e.detail) {
        this.addFood(foodItem)
      } else {
        console.log(1)
        this.delFood(foodItem)
      }
    })
    this.setData({
      selFoods: e.detail
    });

  },
  changeNum(e) {
    const foodItem = e.currentTarget.dataset.record

    this.updataFood(e.currentTarget.dataset.record, e.detail)
    let {
      list
    } = this.data

    list.map(item => {
      if (item.supplierId === foodItem.supplierId) {
        item.foodDTO.map(ele => {
          if (ele.foodId === foodItem.foodId) ele.purchaseNum = e.detail
        })
      }
    })
    this.setData({
      list
    })
  },
  addFood(foodItem) {
    const { supplierId, supplierName, foodId, unit, purchaseNum } = foodItem
    const { applyType } = this.data
    let params = {
      sign: applyType,
      foodId,
      supplierId,
      unit,
      purchaseNum
    }
    if (app.globalData.revise) {
      params.orderId = this.data.orderId
    }
    app.reqFetch.saveFood({
      ...params,
    }, (res) => {
    })
    var menuList = wx.getStorageSync('menuList') || []
    let t = menuList.filter(item => item.supplierId === foodItem.supplierId)
    if (t.length === 0) {
      menuList.push({
        supplierId: supplierId,
        supplierName: applyType === 3 ? '主食领取列表' : supplierName,
        foods: [foodItem],
      })
    } else {
      menuList.map(item => {
        if (item.supplierId === supplierId) {
          item.foods.push(foodItem)
        }
      })
    }
    console.log(menuList)
    wx.setStorageSync('menuList', menuList)

  },
  delFood(foodItem) {
    const { supplierId, supplierName, foodId } = foodItem
    const { applyType } = this.data
    let params = {
      sign: applyType,
      foodId,
      supplierId,
    }
    if (app.globalData.revise) {
      params.orderId = this.data.orderId
    }
    app.reqFetch.deleteFood({
      ...params
    }, (res) => {
    })
  },

  updataFood(foodItem, val) {
    const { applyType } = this.data
    if (foodItem.flag === 1) {
      let params = {
        sign: applyType,
        foodId: foodItem.foodId,
        supplierId: foodItem.supplierId,
        purchaseNum: val
      }
      if (app.globalData.revise) {
        params.orderId = this.data.orderId
      }
      app.reqFetch.updateFood({
        ...params
      }, (res) => {
      })
    }
  },

  handleConfirm(){
    wx.navigateTo({
      url: '/pages/purchase-list/purchase-list',
    })
  },
  handleCancel() {
    wx.navigateBack({
      delta: 1,
    })
  }
})